package org.afun.csms_android.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import org.afun.csms_android.R;
import org.afun.csms_android.domain.Student;
import org.afun.csms_android.domain.Teacher;

public class Student2CourseOperator extends Fragment implements View.OnClickListener {

    private static Context mContext;
    private static Student student;
    private Fragment s2cSelectFragment;
    private Fragment s2cMyCourseFragment;
    private View view;

    public Student2CourseOperator(Context context, Student student) {
        Student2CourseOperator.mContext = context;
        Student2CourseOperator.student = student;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(mContext, R.layout.fragment_student2_course_operator, null);
        }

        // top
        Button s2cSelectBtn = view.findViewById(R.id.s2c_select);
        Button s2cMyCourseBtn = view.findViewById(R.id.s2c_mycourse);
        s2cMyCourseBtn.setOnClickListener(this);
        s2cSelectBtn.setOnClickListener(this);

        s2cSelectFragment = new StudentSelectCourseOperator(mContext, student);
        s2cMyCourseFragment = new StudentShowCourse(mContext, student);

        replaceFragment(s2cMyCourseFragment);
        return view;
    }


    // 动态切换fragment
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transation = fragmentManager.beginTransaction();
        transation.replace(R.id.s2cFrameLayout, fragment);
        transation.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.s2c_select:
                // 选课界面
                replaceFragment(s2cSelectFragment);
                break;
            case R.id.s2c_mycourse:
                // 我的课程界面
                replaceFragment(s2cMyCourseFragment);
                break;
            default:
                break;
        }
    }
}